###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/ConvertDatabase.pm.pl -> ConvertDatabase module                #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::ConvertDatabase;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";
require "Logs.pm.pl";

use strict;

require "ControlPanel/Output/ConvertDatabase.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "CONVERT_DATABASE");

  #----------------------------------------------------------------------#
  # Getting data...                                                      #

  my @Databases;
  
  opendir(DIR, "$SL::PATH/Private/Modules/ControlPanel/Databases") || &Error::CGIError("Error opening directory. $!", "$SL::PATH/Private/Modules/ControlPanel/Databases");
  foreach my $file (grep(/\.pm\.pl$/, readdir(DIR))) {
    my $description = eval("require \"$SL::PATH/Private/Modules/ControlPanel/Databases/$file\"; my \$Source = CP::Database->new(); return \$Source->{'NAME'};")
      || &Error::CGIError("Error evaluating database conversion module. $@", "$SL::PATH/Private/Modules/ControlPanel/Databases/$file");
    my $name = $file;
       $name =~ s/\.pm\.pl$//;
    push(@Databases, { NAME => $name, DESCRIPTION => $description });
  }
  closedir(DIR);

  @Databases = sort { $a->{'NAME'} cmp $b->{'NAME'} } @Databases;

  my %INPUT;
  
  $INPUT{'DATABASES'} = \@Databases;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::ConvertDatabase->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT);
  
  return 1;
}

###############################################################################
# view subroutine
sub view {
  my $self = shift;
  my %in = (DB => undef, ERROR => "", @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "CONVERT_DATABASE");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  if (!$SL::QUERY{'FORM_DATABASE'} || !(-e "$SL::PATH/Private/Modules/ControlPanel/Databases/$SL::QUERY{'FORM_DATABASE'}.pm.pl")) {
    $self->show(DB => $in{'DB'});
    return 1;
  }

  #----------------------------------------------------------------------#
  # Preparing data...                                                    #

  require "$SL::PATH/Private/Modules/ControlPanel/Databases/$SL::QUERY{'FORM_DATABASE'}.pm.pl";
  my $Source = CP::Database->new(DB => $in{'DB'});
  
  my %INPUT;
  
  $INPUT{'FIELDS'} = $Source->PrintFields($in{'ERROR'});

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::ConvertDatabase->new();

  &Standard::PrintHTMLHeader();
  print $Skin->view(input => \%INPUT);
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "CONVERT_DATABASE");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  if (!$SL::QUERY{'FORM_DATABASE'} || !(-e "$SL::PATH/Private/Modules/ControlPanel/Databases/$SL::QUERY{'FORM_DATABASE'}.pm.pl")) {
    $self->show(DB => $in{'DB'});
    return 1;
  }

  #----------------------------------------------------------------------#
  # Preparing data...                                                    #

  require "$SL::PATH/Private/Modules/ControlPanel/Databases/$SL::QUERY{'FORM_DATABASE'}.pm.pl";
  my $Source = CP::Database->new(DB => $in{'DB'});

  if (my $error = $Source->CheckFields()) {
    $self->view(DB => $in{'DB'}, ERROR => $error);
    return 1;
  }

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::ConvertDatabase->new();

  &Standard::PrintHTMLHeader();  

  unless ($SL::QUERY{'do'}) {
    print $Skin->do();
    return 1;
  }

  my $Log = Logs->new(PRINT => 1);
  
  my $Exit = eval {
    sub {
      $Log->Footer();
      exit;
    }
  };
  
  $Log->Header();
  $Source->Main($Log, $Exit);
  &$Exit;
}

1;